﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using CJ_Logging;

namespace CJ_Logging.Test
{
    static class Program
    {
        private const string UNHANDLER_EXCEPTION_ALERT_MESSAGE = "系统出现未处理的异常，请查看日志文件！";
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            
            try
            {
                //处理未捕获的异常   
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常   
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常   
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                var f = new Form1();
                Application.Run(f);
            }
            catch (Exception ex)
            {
                ex.Data.Add("AppVersion", Application.ProductVersion);
                string str = CatchException.Instance.GetExceptionDesc(ex);
                CatchException.Instance.Save(str);
                MessageBox.Show(UNHANDLER_EXCEPTION_ALERT_MESSAGE, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception error = e.Exception as Exception;
            error.Data.Add("应用程序版本", Application.ProductVersion);
            error.Data.Add("操作系统名称", Environment.OSVersion.VersionString);
            error.Data.Add("程序所在目录", Environment.CurrentDirectory);
            error.Data.Add("FrameWork版本", Environment.Version);
            error.Data.Add("登录帐号", Environment.UserName);
            error.Data.Add("域名称", Environment.UserDomainName);


            string str = CatchException.Instance.GetExceptionDesc(error);
            CatchException.Instance.Save(str);
            MessageBox.Show(UNHANDLER_EXCEPTION_ALERT_MESSAGE, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception error = e.ExceptionObject as Exception;
            error.Data.Add("AppVersion", Application.ProductVersion);
            string str = CatchException.Instance.GetExceptionDesc(error);
            CatchException.Instance.Save(str);
            MessageBox.Show(UNHANDLER_EXCEPTION_ALERT_MESSAGE, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        
        #region 暂时不用的代码


        //string str = "";
        ////string strDateInfo = "出现应用程序未处理的异常：" + DateTime.Now.ToString() + "\r\n";
        //Exception error = e.Exception as Exception;
        //if (error != null)
        //{
        //    str = string.Format("异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
        //         error.GetType().Name, error.Message, error.StackTrace);
        //}
        //else
        //{
        //    str = string.Format("应用程序线程错误:{0}", e);
        //}

        ///// <summary>
        ///// 写文件
        ///// </summary>
        ///// <param name="str"></param>
        //static void writeLog(string str)
        //{
        //    if (!Directory.Exists("ErrLog"))
        //    {
        //        Directory.CreateDirectory("ErrLog");
        //    }

        //    using (StreamWriter sw = new StreamWriter(@"ErrLog\ErrLog.txt", true))
        //    {
        //        sw.WriteLine(str);
        //        sw.WriteLine("---------------------------------------------------------");
        //        sw.Close();
        //    }
        //}

    #endregion
    }
}
